class Solution:
    def minPathSum(self, grid) -> int:
        dp = [[None for y in range(len(grid[x]))] for x in range(len(grid))]
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if i == 0:
                    if j == 0:
                        dp[i][j] = grid[i][j]
                    else:
                        dp[i][j] = dp[i][j - 1] + grid[i][j]
                elif j == 0:
                    dp[i][j] = dp[i - 1][j] + grid[i][j]
                else:
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
        return dp[-1][-1]


if __name__ == '__main__':
    s = Solution()
    grid = [
        [1, 3, 1],
        [1, 5, 1],
        [4, 2, 1]
    ]
    r = s.minPathSum(grid)
    print(r)
